﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;

using CommunityServer.Controls;

using CSLive.LiveTranslator;

using Control=CSLive.ControlPanel.Editor.Control;

namespace CSLive.Controls
{
    public class Translator : WrappedContentBase, ICSLiveControl
    {
        [Bindable( true )]
        [DefaultValue( "DropDownList" )]
        [Localizable( true )]
        public TranslatorView ViewMode
        {
            get { return ( ViewState[ "ViewMode" ] == null ) ? TranslatorView.DropDownMenu : ( TranslatorView ) ViewState[ "ViewMode" ]; }
            set { ViewState[ "ViewMode" ] = value; }
        }

        #region ICSLiveControl Members

        public Dictionary< string, Control > EditorControls
        {
            get
            {
                Dictionary< string, Control > dictionary;
                dictionary = new Dictionary< string, Control >
                                 {
                                     {
                                         "Translator Drop Down", new Control( "Translator Drop Down",
                                                                              "Windows Live Translator Drop Down Menu",
                                                                              false,
                                                                              "<CSLive:Translator ViewMode=\"DropDownMenu\" runat=\"server\" />" )
                                         },
                                     {
                                         "Translator Hyperlinks", new Control( "Translator Hyperlinks",
                                                                               "Windows Live Translator Hyperlinks",
                                                                               false,
                                                                               "<CSLive:Translator ViewMode=\"HyperLinks\" runat=\"server\" />" )
                                         },
                                     {
                                         "Translator Flags", new Control( "Translator Flags",
                                                                          "Windows Live Translator Flags",
                                                                          false,
                                                                          "<CSLive:Translator Tag=\"Div\" ContainerId=\"translatortags\" ViewMode=\"Flags\" runat=\"server\" />" )
                                         }
                                 };
                return dictionary;
            }
        }

        public string Name
        {
            get { return "Live Translator"; }
        }

        public Uri DocsUri
        {
            get { return new Uri( "http://communityserverlive.com/controls/translator/default.aspx" ); }
        }

        public string Description
        {
            get { return "Windows Live Translator Controls"; }
        }

        public string Group
        {
            get { return "Live Translator"; }
        }

        #endregion

        protected override void BindDefaultContent( System.Web.UI.Control control, IDataItemContainer dataItemContainer )
        {
            string[] keys = LiveConfiguration.Instance.getConfigSetting( Name, "LanguageKeys" ).Value.ToString().Split( new[] {','} );
            string[] values = LiveConfiguration.Instance.getConfigSetting( Name, "LanguageValues" ).Value.ToString().Split( new[] {','} );
            switch ( ViewMode )
            {
                case TranslatorView.HyperLinks:
                    for ( int x = 0; x < keys.Length; x++ )
                    {
                        if ( keys[ x ] != LiveConfiguration.Instance.getConfigSetting( Name, "SiteLanguage" ).Value.ToString().ToLower() )
                        {
                            control.Controls.Add( new LiteralControl( string.Format( "<a href=\"http://www.windowslivetranslator.com/BV.aspx?ref=AddIn&lp={0}_{1}&a={3}\" onclick=\"window.open(this.href); return false;\">{2}</a>", LiveConfiguration.Instance.getConfigSetting( Name, "SiteLanguage" ).Value.ToString().ToLower(), keys[ x ], Context.Server.HtmlEncode( values[ x ] ), Context.Server.UrlPathEncode( string.Format( "{0}://{1}{2}", Context.Request.Url.Scheme, Context.Request.Url.Host, Context.Request.RawUrl ) ) ) ) );
                            if ( x != keys.Length - 1 ) control.Controls.Add( new LiteralControl( LiveConfiguration.Instance.getConfigSetting( Name, "Seperator" ).Value.ToString() ) );
                        }
                    }
                    break;
                case TranslatorView.Flags:
                    for ( int x = 0; x < keys.Length; x++ )
                    {
                        if ( keys[ x ] != LiveConfiguration.Instance.getConfigSetting( Name, "SiteLanguage" ).Value.ToString().ToLower() )
                            control.Controls.Add( new LiteralControl( string.Format( "<a href=\"http://www.windowslivetranslator.com/BV.aspx?ref=AddIn&lp={0}_{1}&a={3}\" onclick=\"window.open(this.href); return false;\"><img src=\"{4}\" border=\"0\" alt=\"{2}\" /></a>", LiveConfiguration.Instance.getConfigSetting( Name, "SiteLanguage" ).Value.ToString().ToLower(), keys[ x ], Context.Server.HtmlEncode( values[ x ] ), Context.Server.UrlPathEncode( string.Format( "{0}://{1}{2}", Context.Request.Url.Scheme, Context.Request.Url.Host, Context.Request.RawUrl ) ), ResolveClientUrl( string.Format( "{0}flag_{1}.gif", LiveConfiguration.Instance.getConfigSetting( Name, "FlagsUrl" ).Value, keys[ x ] ) ) ) ) );
                    }
                    break;
                default:
                    control.Controls.Add( new LiteralControl( "<script type=\"text/javascript\" src=\"http://www.windowslivetranslator.com/TranslatePageLink.aspx?pl=en\"></script>" ) );
                    break;
            }
        }
    }
}